//
//  confirmView.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/23.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "confirmView.h"
#import "popV.h"
#import "returnMoneyResultVC.h"
#import "setTradingPassword.h"
#import <AlipaySDK/AlipaySDK.h>
@implementation confirmView


- (void)awakeFromNib {
    [super awakeFromNib];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(paySuccessfulAction) name:@"paySuccessful" object:nil];
    self.confirm_btn.layer.masksToBounds = YES;
    self.confirm_btn.layer.cornerRadius = 22;
    payWay = @"1";
}

- (IBAction)dissClick:(id)sender {//取消
    [[helpPublic getCurrentViewController:self] dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)balanceClick:(id)sender {//选择余额支付方式
    UIButton *btn = (UIButton *)sender;//redCircle
    [btn setImage:[UIImage imageNamed:@"redRound"] forState:0];
    for (UIView *view in self.subviews) {
        if (view.tag == 1000) {
            UIButton *b = (UIButton *)view;
            [b setImage:[UIImage imageNamed:@"redCircle"] forState:0];
        }
    }
    payWay = @"1";
}
- (IBAction)bankClick:(id)sender {//支付宝
    UIButton *btn = (UIButton *)sender;//redCircle
    [btn setImage:[UIImage imageNamed:@"redRound"] forState:0];
    for (UIView *view in self.subviews) {
        if (view.tag == 1001) {
            UIButton *b = (UIButton *)view;
            [b setImage:[UIImage imageNamed:@"redCircle"] forState:0];
        }
    }
    payWay = @"2";
}

- (IBAction)confirmClick:(id)sender {//确认按钮
    if ([payWay isEqualToString:@"1"]) { //支付方式 1=余额 2=支付宝
        [self isSetTradePassword]; // 余额支付路径
    }else if ([payWay isEqualToString:@"2"]) {
        if ([self.payType isEqualToString:@"1"]) {//支付类型 1=服务费 2=余额
            [self aliPayServiceMoney];
        }else {
            [self aliPayReimbursement];
        }
    }
}
#pragma mark --- 支付宝支付
// 还款
- (void)aliPayReimbursement {
    NSDictionary *dic = @{
                          @"token":USERTOKEN,
                          @"money":self.payMoney,
                          @"repaymentIdToString":self.ID,
                          @"flg" : self.ordType
                          };
    __weak typeof(self) weakSelf = self;
    NSString *url = [NSString stringWithFormat:@"%@", [[apiManger shareApiManger] aliPayReimbursement]];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [weakSelf playClickWithOrder:responseObject [@"data"]];
        }else{
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
// 支付服务费
- (void)aliPayServiceMoney {
    NSDictionary *dic = @{
                          @"token":USERTOKEN,
                          @"hongbaoId":@""
                          };
    NSString *url = [NSString stringWithFormat:@"%@%@", [[apiManger shareApiManger] aliPayPayServiceCharge], self.ID];
    __weak typeof(self)weakSelf = self;
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [weakSelf playClickWithOrder:responseObject [@"data"]];
        }else{
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
- (void)playClickWithOrder:(NSString *)order {
    [[AlipaySDK defaultService] payOrder:order fromScheme:AppScheme callback:^(NSDictionary *resultDic) {
        NSLog(@"reslut = %@",resultDic);
    }];
}

- (void)paySuccessfulAction {
    [[helpPublic getCurrentViewController:self] dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark --- 余额支付
//判断是否设置交易密码
- (void)isSetTradePassword {
    NSDictionary *dic = @{
                          @"token":USERTOKEN,
                          };
    __weak typeof(self) weakSelf = self;
    NSString *url = [NSString stringWithFormat:@"%@", [[apiManger shareApiManger] isSetPayPassword]];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            NSString *flg = [NSString stringWithFormat:@"%@", responseObject[@"flg"]];
            if ([flg isEqualToString:@"1"]) {
                [weakSelf popTradPasswordView];
            }else {
                [weakSelf popSetTradView];
            }
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
- (void)popSetTradView {
    setTradingPassword *VC = [setTradingPassword new];
    [[helpPublic getCurrentViewController:self].navigationController pushViewController:VC animated:YES];
}
- (void)popTradPasswordView {//输入支付密码页面
    [[helpPublic getCurrentViewController:self] dismissViewControllerAnimated:YES completion:nil];
    popV *V = [[NSBundle mainBundle] loadNibNamed:@"popV"owner:nil options:nil].firstObject;
    V.cblock = ^(NSInteger t, NSString *password){
        if (t == 0) {
            
        }else {
            self.password = password;
            if ([self.payType isEqualToString:@"1"]) {// 支付类型 1=服务费 2=还款
                [self serviceMoney];
            }else {
                [self confirmReturnMoney];
            }
        }
    };
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:V];
    [V mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(@0);
    }];
}
//分期还款(余额)
- (void)confirmReturnMoney {
    
    NSDictionary *dic = @{
                          @"token":USERTOKEN,
                          @"money":self.payMoney,
                          @"payPassWord":self.password,
                          @"repaymentIdToString":self.ID,
                          @"flg" : self.ordType
                          };
    NSString *url = [NSString stringWithFormat:@"%@", [[apiManger shareApiManger] balanceReturnMoney]];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        resultVC *VC = [resultVC new];
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            VC.resultType = @"3";
            [nav pushViewController:VC animated:YES];
            [self postNotic];
        }else {
            VC.resultType = @"4";
            [nav pushViewController:VC animated:YES];
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
//支付服务费（余额）
- (void)serviceMoney {
    NSDictionary *dic = @{
                          @"token":USERTOKEN,
                          @"payPassWord":self.password,
                          @"hongbaoId":@""
                          };
    NSString *url = [NSString stringWithFormat:@"%@%@", [[apiManger shareApiManger] zhiJieZhiFuPay], self.ID];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"支付服务费成功"]];
            [self postNotic];
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}

- (void)postNotic {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"paySuccessful" object:nil];
}
- (void)dealloc {
   
}

@end
